package listeners;

import gridview.GridViewActivity;

import java.util.Set;

import playlistData.PlaylistManager;
import utils.DialogCreator;
import xml.NodeUtilities;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;

import com.unipv14.androidgallery.IListsRefresher;
import com.unipv14.androidgallery.ImageShower;
import com.unipv14.androidgallery.R;

public class MenuListener implements OnClickListener {
	private Activity currentActivity;
	private PlaylistManager manager;
	private IListsRefresher refresher;
	private AlertDialog dialog;
	private DialogCreator dialogCreator;
	

	public MenuListener(Activity currentActivity, PlaylistManager manager,
			IListsRefresher refresher) {
		this.currentActivity = currentActivity;
		this.manager = manager;
		this.refresher = refresher;
		dialogCreator = new DialogCreator();
	}

	public void createMenu(Menu menu) {
		MenuInflater inflater = currentActivity.getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
	}

	public void buttonPressed(MenuItem item) {
		if (item.getItemId() == R.id.addP) { 
			dialogCreator.createDialogWindow(currentActivity, dialog, R.string.add_playlist
					, R.layout.add_playlist_layout,this);
			
		} else if (item.getItemId() == R.id.showGrid) {
			if (!manager.isPlaylistList()) {
				Set<String> set = manager.getMap().getPlaylist().keySet();
				String[] list = new String[set.size()];
				list = set.toArray(list);
				ImageShower.showImage(list, currentActivity,list[0],GridViewActivity.class);
			}
		}
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		EditText textField = (EditText)((AlertDialog)dialog)
				.findViewById(R.id.playlistName);
		String text = textField.getText().toString();
		if (text.trim().length() > 0) {
			manager.addPlaylist(text, NodeUtilities.defaultPlaylistLocation);
			refresher.refreshList();
		}
	}
}
